package eHealth.android.client;

import eHealth.android.REST.RESTMethods;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

/**
 * 
 * @author Thomas Rouvinez
 * Date 08.03.2012
 *
 * Goal : create a login interface with check in REST
 * for the authentification. This is the first page
 * displayed in the application.
 *
 */
public class Logon extends Activity implements OnClickListener{

	/**
	 * Graphical component for the login button.
	 */
	private Button login;
	
	/**
	 * Graphical components for user name and password input.
	 */
	private EditText userName, password;
	
	/**
	 * Context of this current screen.
	 */
	public static Context context;

	/**
	 * Boolean acknowledgment for user login.
	 */
	private boolean authentified = false;

	// ----------------------------------------------------------------------------------------------
	// Android methods.
	// ----------------------------------------------------------------------------------------------

	/** Called when the activity is first created.<br>
	 * Set the window size to fullscreen and assign the layout - GUI */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Set full screen application.
		requestWindowFeature(Window.FEATURE_NO_TITLE);   
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,    
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// Set content view.
		setContentView(R.layout.main);

		// Assign the buttons.
		assignGUI();
		
		// Get context.
		context = this;
	}

	/** Called when the activity starts once it is created.<br>
	 * Allows to set the listeners properly. */
	@Override
	protected void onStart(){
		super.onStart();

		// Add listeners to the buttons.
		setListeners();
	}
	
	// ----------------------------------------------------------------------------------------------
	// Option menu.
	// ---------------------------------------------------------------------------------------------- 
	
	/**
	 * Create option menu entries.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 1000, 1, "Help").setIcon(android.R.drawable.ic_menu_help);
		menu.add(0, 1001, 1, "Exit").setIcon(android.R.drawable.ic_lock_power_off);

		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * Set listeners and actions to take for menu entries buttons.
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch (item.getItemId()) {
		
		case 1000:
			// Help button.
			final AlertDialog alertDialog = new AlertDialog.Builder(Logon.this).create();
			alertDialog.setTitle(getString(R.string.MenuEntryHelpLabel));
			alertDialog.setMessage(getString(R.string.MenuEntryHelpText));
			
			// OK button of the help dialog.
			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					// Do nothing.
				}
			});
			alertDialog.setIcon(R.drawable.ic_launcher);
			alertDialog.show();
			break;
			
		case 1001:
			// Exit button.
			this.finish();
			android.os.Process.killProcess(android.os.Process.myPid()) ;
			break;

		// Default action.
		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	// ----------------------------------------------------------------------------------------------
	// Methods.
	// ---------------------------------------------------------------------------------------------- 

	/**
	 * Link local graphical objects to GUI objects.
	 */
	private void assignGUI(){
		login = (Button)this.findViewById(R.id.button_Login);
		userName = (EditText)findViewById(R.id.editText_UserName);
		password = (EditText)findViewById(R.id.EditText_Password);
	}

	/**
	 * Method to set the listener for buttons.
	 */
	private void setListeners(){
		login.setOnClickListener(this);
	}

	/**
	 * Method to regroup all buttons' listeners and assign them.
	 */
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_Login:	
			try {
				// Check with the server if the user can access the application.
				if(userName.getText().length() != 0 && password.getText().length() != 0){
					authentified = RESTMethods.login(userName.getText().toString(), password.getText().toString());
					
					// Decide course of action depending on the success of the authentication.
					if(authentified == true){
						// Load next activity.
						Intent menu = new Intent(this, eHealth.android.client.Menu.class);
						this.startActivityForResult(menu, 100);

						// Forbid the user to reenter the login while pressing
						// the back button.
						this.finish() ;
					}
					else{
						// Reset userName and Password fields.
						userName.setText("");
						password.setText("");
					}
				}
			} catch (Exception e) {
				// Give user information in case the login is not granted.
				//Toast.makeText(this, R.string.ErrorMessageLogonConnection + " \nERR\n" + e.toString(), 500);
			}

		default:
			break;
		}	
	}
}